#ifndef PIECHARTWIDGET_H
#define PIECHARTWIDGET_H

#include <QWidget>
#include <QPainter>
#include <QVector>
#include <QColor>
#include <QString>
#include <QLayout>
#include <QLabel>
#include <QMap>
#include <QPen>

class PieChartWidget : public QWidget
{
    Q_OBJECT

public:
    explicit PieChartWidget(QWidget *parent = nullptr);

    // 设置四个数据的方法
    void setData(double value1, double value2, double value3, double value4);
    void setData(const QVector<double> &values);

    // 设置数据标签
    void setLabels(const QString &label1, const QString &label2,
                   const QString &label3, const QString &label4);
    void setLabels(const QVector<QString> &labels);

    // 设置颜色
    void setColors(const QColor &color1, const QColor &color2,
                   const QColor &color3, const QColor &color4);
    void setColors(const QVector<QColor> &colors);

    // 设置标题
    void setTitle(const QString &title);
    QString title() const;

    // 设置是否显示图例
    void setShowLegend(bool show);
    bool showLegend() const;

    // 设置饼图半径
    void setPieRadius(int radius);
    int pieRadius() const;

    // 设置图例位置
    enum LegendPosition { Right, Bottom, Top, Left };
    void setLegendPosition(LegendPosition position);
    LegendPosition legendPosition() const;

    // 设置是否显示百分比
    void setShowPercentage(bool show);
    bool showPercentage() const;

    // 设置高亮效果
    void setHighlightEnabled(bool enable);
    bool highlightEnabled() const;

    // 清除数据
    void clear();

    // 刷新图表
    void updateChart();

protected:
    void paintEvent(QPaintEvent *event) override;
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void leaveEvent(QEvent *event) override;

private:
    struct PieSlice {
        double value;
        double percentage;
        double startAngle;
        double spanAngle;
        QColor color;
        QString label;
        bool highlighted;

        PieSlice() : value(0), percentage(0), startAngle(0), spanAngle(0),
            highlighted(false) {}
    };

    void calculatePercentages();
    void drawPieChart(QPainter &painter);
    void drawLegend(QPainter &painter);
    QPoint getPieCenter() const;
    bool isPointInSlice(const QPoint &point, int sliceIndex) const;
    void updateHighlight(int sliceIndex);

    QVector<PieSlice> slices;
    QString chartTitle;
    bool m_showLegend;
    bool m_showPercentage;
    bool m_highlightEnabled;
    int m_pieRadius;
    LegendPosition m_legendPosition;
    int highlightedSlice;
    QFont titleFont;
    QFont legendFont;
};

#endif
